#include <logger/logger.h>
#include <QTextStream>


namespace Opl { namespace Core {


    //Logger* Logger::_logger = 0;
#if 0
    Logger::LoggerPtr Logger::_loggerPtr = LoggerPtr();

    class LoggerPrivate{
        public:

            LoggerPrivate() {}
            ~LoggerPrivate()
            {
                delete stream;

                logFile->close();
                delete logFile;
            }

            LoggerItem::Level level;
            QTextStream * stream;
            QFile* logFile;
    };



    Logger::Logger(const QString& filename, LoggerItem::Level level/* = LoggerItem::INFO*/, Mode mode/* = append*/)
        :QObject(0), O_PRIVATE_CREATE(Logger)
    {
        O_D(Logger);
        d->level = level;
        d->stream = 0;

        d->logFile = new QFile(filename);
        if (!d->logFile->open(QFile::WriteOnly | (QFile::OpenMode)mode)){
            throw LoggerException("can't open log file:" + d->logFile->errorString());
        }

        d->stream = new QTextStream(d->logFile);
    }

    Logger::~Logger()
    {
       O_PRIVATE_DELETE;
    }

    void Logger::Initialize(const QString& filename, const QString& level/* = "INFO"*/, const QString& mode/* = "APPEND"*/)
    {
        LoggerItem::Level ll;
        QString lvl = level.toUpper();
        if (lvl == "FATAL"){
            ll = LoggerItem::FATAL;
        }else if (lvl == "ERROR" || lvl == "ERR"){
            ll = LoggerItem::ERR;
        }else if (lvl == "WARNING"){
            ll = LoggerItem::WARNING;
        }else if (lvl == "DEBUG"){
            ll = LoggerItem::DEBUG;
        }else if (lvl == "TRACE"){
            ll = LoggerItem::TRACE;
        }else{
            ll = LoggerItem::INFO;
        }

        Mode lm;
        QString m = mode.toUpper();
        if (m == "TRUNCATE"){
            lm = truncate;
        }else{
            lm = append;
        }

        Initialize(filename, ll, lm);
    }

    void Logger::Initialize(const QString& filename, LoggerItem::Level level/* = LoggerItem::INFO*/, Mode mode/* = append*/)
    {
        if (Logger::_loggerPtr.data() == NULL){
            Logger::_loggerPtr = LoggerPtr(new Logger(filename, level, mode));
        }
    }

    Logger& Logger::logger()
    {
        return *_loggerPtr;
    }

    void Logger::LogMessage(const LoggerItem& msg)
    {
        O_D(Logger);
        if (d->level >= msg.level){

            *d->stream << QDateTime::currentDateTime().toString("[yyyy-MM-dd hh:mm:ss]") << msg.buffer << "\n";
            d->logFile->flush();
            qDebug() << msg.buffer.toAscii();
            emit message(msg.level, msg.buffer);
        }
    }
#endif 

    LoggerItem::LoggerItem()
        :QDebug(&buffer)
    {
    }

    LoggerItem::~LoggerItem()
    {
		switch(level)
		{
		case FATAL:
			logger()->fatal(buffer);
			break;
		case ERR:
			logger()->error(buffer);
			break;
		case WARNING:
			logger()->warn(buffer);
			break;
		case INFO:
			logger()->info(buffer);
			break;
		case DEBUG:
			logger()->debug(buffer);
			break;
		default:
			logger()->trace(buffer);
			break;
		}

     //   Logger::logger().LogMessage(*this);
    }

    LoggerItem& LoggerItem::Trace()
    {
        level = TRACE;
        //*this << "[trace]";
        return *this;
    }


    LoggerItem& LoggerItem::Debug()
    {
        level = DEBUG;
        //*this << "[DBG]";
        return *this;
    }

    LoggerItem& LoggerItem::Info()
    {
        level = INFO;
        //*this << "[INF]";
        return *this;
    }


    LoggerItem& LoggerItem::Warning()
    {
        level = WARNING;
        //*this << "[WARNING]";
        return *this;
    }

    LoggerItem& LoggerItem::Error()
    {
        level = ERR;
        //*this << "[ERROR]";
        return *this;
    }

    LoggerItem& LoggerItem::Fatal()
    {
        level = FATAL;
        //*this << "[FATAL]";
        return *this;
    }

}; };
